Skip to content

Improved cost calculation to account for cache writes and long context - #3

Open
Helyk wants to merge 1 commit into
mainfrom
cost
Open

Improved cost calculation to account for cache writes and long context#3
Helyk wants to merge 1 commit into
mainfrom
cost

Conversation

@Helyk

@Helyk Helyk commented Jul 19, 2026

Copy link
Copy Markdown
Collaborator

Removed getmodels command and associated code
Added cost logging for streaming
TODO: service tier based pricing, logging for background polling, for intermediate responses with auto tooling

Removed getmodels command and associated code
Added cost logging for streaming
TODO: service tier based pricing, logging for background polling, for intermediate responses with auto tooling

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates model pricing/cost computation to account for cache writes and long-context tiered rates, and extends usage/cost logging so streaming Responses can log token usage and computed USD cost when a stream completes.

Changes:

  • Introduce richer Usage/ResponseUsage shapes (incl. cache_write_tokens) and propagate usage onto responses.Response.
  • Add streaming-completion cost logging for SSE and WebSocket Responses streams.
  • Replace generated model pricing table + generator command with handwritten pricing structures and a models.Data[model].Cost(usage) helper.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
responses/streaming/types.go Adds ResponseUsage and attaches it to streaming Response payloads.
responses/service.go Adds Response.Usage and a shared Usage struct for Responses API usage counts.
README.md Documents cost calculation behavior and how to compute cost from streaming usage.
models/text.go Introduces pricing struct helpers and Cost(usage) to compute USD cost including cache writes and long-context pricing.
internal/inresponses/ws.go Logs streaming cost for WebSocket events upon receipt.
internal/inresponses/client.go Uses new Cost(usage) for non-streaming responses and logs cost for completed SSE streams; propagates usage through follow-up requests.
internal/inchat/api.go Extends Chat Completions usage parsing with cache write tokens and updates cost calculation accordingly.
internal/cmd/getmodels/main.go Removes the getmodels generator command.
internal/cmd/getmodels/api.go Removes models.dev API integration used by the generator.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread models/text.go
Comment on lines +226 to +231
details := usage.InputTokensDetails
uncachedInput := usage.InputTokens - details.CachedTokens - details.CacheWriteTokens
return float64(uncachedInput)*priceIn +
float64(details.CachedTokens)*priceCachedIn +
float64(details.CacheWriteTokens)*priceCacheWrite +
float64(usage.OutputTokens)*priceOut
Comment thread internal/inchat/api.go
Comment on lines +113 to +118
details := usage.PromptTokensDetails
uncachedInput := usage.Prompt - details.CachedTokens - details.CacheWriteTokens
return float64(uncachedInput)*priceIn +
float64(details.CachedTokens)*priceCachedIn +
float64(details.CacheWriteTokens)*priceCacheWrite +
float64(usage.Completion)*priceOut, true
pricing, ok := models.Data[model]
if !ok {
c.Log.Warn(fmt.Sprintf("No pricing for found model '%s'", resp.Model))
c.Log.Warn(fmt.Sprintf("No pricing for found model '%s'", model))
Comment thread models/text.go
// Package models contains constants and pricing data for all OpenAI models.
package models

import "github.com/unkn0wncode/openai/responses"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants